﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;

namespace MapControl.Bing
{
    internal class BingImageManager : CommonImageManager
    {
        public string ApiKey { get; set; }

        private string cachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MapControl/cache/Bing");
        private static IFormatProvider decimalFormatProvider = new NumberFormatInfo() { NumberDecimalSeparator = "." };

        public override string CachePath
        {
            get
            {
                return cachePath;
            }
        }

        protected override string GetUrl(GeoFramework.Position position, int zoomLevel, MapType mapType)
        {
            return string.Format("http://dev.virtualearth.net/REST/V1/Imagery/Map/{4}/{0},{1}/{2}?mapSize=256,256&key={3}",
                position.Latitude.DecimalDegrees.ToString(decimalFormatProvider),
                position.Longitude.DecimalDegrees.ToString(decimalFormatProvider),
                zoomLevel, ApiKey, GetMapType(mapType));
        }

        private static string GetMapType(MapType mapType)
        {
            switch (mapType)
            {
                case MapType.Roadmap:
                    return "Road";
                case MapType.Satellite:
                    return "Aerial";
                case MapType.Hybrid:
                    return "AerialWithLabels";
                default:
                    return "Road";
            }
        }
    }
}
